package com.luzhikun.servlet; /**
 * ClassName: ${NAME}
 * Description: ${Description}
 * date: 2021/4/28 20:40
 *
 * @author luzhikun
 * @version
 * @since JDK 1.8
 */

import com.luzhikun.model.Student;
import com.luzhikun.service.IStudentService;
import com.luzhikun.service.StudentServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登录control层
 */
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //0. 获取session对象(如果有使用，如果没有创建)
        HttpSession session = request.getSession();
        //1，获取浏览器请求参数
        String num = request.getParameter("num");
        String password = request.getParameter("password");
        if (num == null || "".equals(num.trim())) {
            session.setAttribute("message", "学号输入错误");
            return;
        }
        if (password == null || "".equals(password)) {
            session.setAttribute("message", "学号输入错误");
            return;
        }
        int number = Integer.valueOf(num);
        System.out.println(number);
        System.out.println(password);
        //2.创建学生对象
        Student student = null;
        //3.创建service抽象实现类对象
        IStudentService stuSe = new StudentServiceimpl();
        //4.调用checkLogin()方法
        student = stuSe.checkLogin(number, password);
        //5.判断登录失败
        if (student == null) {
            session.setAttribute("message", "学号密码输入错误");
            System.out.println("sss");
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }
        //6.判断登录成功
        if (student != null) {
            session.setAttribute("message", "学号密码输入正确");
            System.out.println("bbb");
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }

    }
}
